package cn.stylefeng.guns.modular.stage.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.stage.entity.StageData;
import cn.stylefeng.guns.modular.stage.mapper.StageDataMapper;
import cn.stylefeng.guns.modular.stage.model.params.StageDataParam;
import cn.stylefeng.guns.modular.stage.model.result.StageDataResult;
import  cn.stylefeng.guns.modular.stage.service.StageDataService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 阶段视频 服务实现类
 * </p>
 *
 * @author CHENCHEN
 * @since 2022-07-21
 */
@Service
public class StageDataServiceImpl extends ServiceImpl<StageDataMapper, StageData> implements StageDataService {

    @Override
    public void add(StageDataParam param){
        StageData entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(StageDataParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(StageDataParam param){
        StageData oldEntity = getOldEntity(param);
        StageData newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public StageDataResult findBySpec(StageDataParam param){
        return null;
    }

    @Override
    public List<StageDataResult> findListBySpec(StageDataParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(StageDataParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public StageDataResult selectAllById(Long id) {
        return this.baseMapper.selectAllById(id);
    }

    private Serializable getKey(StageDataParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private StageData getOldEntity(StageDataParam param) {
        return this.getById(getKey(param));
    }

    private StageData getEntity(StageDataParam param) {
        StageData entity = new StageData();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
